

const { Op, Sequelize } = require("sequelize");
const ArticleCollect = require('../model/article_collect.model');
const moment = require('moment');
const seq = require('../db/seq')
class collectService {
    // 访问量 
    /**
     * 如果当日内没存过,则创建一条，如果当日存过在原有基础上增加个数
     * */
    async saveCollectService(param) {
        const { id = 0, article_id, author, authoruser_id, operateuser, title, operateuser_id } = param
        // 查询未删除的
        const where = {
            article_id: { [Op.eq]: article_id },
            operateuser_id: { [Op.eq]: operateuser_id },
            is_delete: { [Op.eq]: 0 }
        }
        const where_have_delete = {
            article_id: { [Op.eq]: article_id },
            operateuser_id: { [Op.eq]: operateuser_id },
            is_delete: { [Op.eq]: 1 }
        }
        const res = await ArticleCollect.findOne({
            where
        })
        const res_hd = await ArticleCollect.findOne({
            where_have_delete
        })
        if (!res) {
            const data = await ArticleCollect.create({ id: id, article_id: article_id, author: author, authoruser_id: authoruser_id, operateuser: operateuser, title: title, operateuser_id: operateuser_id })
            return 'success'
        } else if (res_hd) {
            // 这是删除了的然后再次添加 因为是改变is_delete的状态这种假删除
            try {
                await cancelArticleCollectService({ article_id, operateuser_id, is_delete: 0 })
                return 'success'
            } catch {
                return 'error'
            }
        } else {
            return 'have'

        }

    }
    // 获取当篇文章的收藏
    async getArticleCollectService(param) {
        const { article_id = '', operateuser_id } = param
        let result;
        await seq.query(`SELECT COUNT(article_id) AS count FROM ArticleCollects WHERE article_id=${article_id} AND operateuser_id=${operateuser_id} And is_delete= 0`, { type: seq.QueryTypes.SELECT }).then((res) => {
            result = res
        })
        return result
    }

    // 取消收藏
    async cancelArticleCollectService(param) {
        const { article_id, operateuser_id, is_delete = 1 } = param
        const res = await ArticleCollect.update({ is_delete: is_delete }, {
            where: {
                article_id: article_id,
                operateuser_id: operateuser_id
            }
        })
        return res[0] > 0 ? 'success' : 'error'
    }
    async getCollectListService(param) {
        const { operateuser_id } = param
        let result;
        try {
            //   seq是定义的数据库实例
            await seq.query(`SELECT * FROM Articles WHERE id IN (SELECT article_id FROM ArticleCollects WHERE operateuser_id = ${operateuser_id} AND is_delete=0)`, { type: seq.QueryTypes.SELECT }).then((res) => {
                //  return 不能写到promise中
                result = res
            })
            return result
        } catch (err) {
            console.log(err)
            return err
        }
    }
}
// 导出后 在控制器controller中使用
module.exports = new collectService()